package com.hanhai.material.device.utils.kingdee;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.KingdeeResult;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysProductRecordDetentionRecord;
import com.hanhai.material.device.model.TokenModel;
import com.hanhai.material.device.model.TransferModel;
import com.hanhai.material.device.model.transfer.*;
import com.hanhai.material.device.service.SysProductRecordDetentionRecordService;
import com.hanhai.material.device.service.SysProductRecordIntoService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hanhai.material.device.entity.table.SysProductRecordDetentionRecordTableDef.SYS_PRODUCT_RECORD_DETENTION_RECORD;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;

@Component
@Log4j2
public class TransferDataUtil {
    @Value("${kingdee.baseUrl}")
    String baseUrl;
    @Value("${kingdee.authorityCode}")
    String authorityCode;
    @Autowired
    SysProductRecordIntoService sysProductRecordIntoService;

    @Autowired
    SysProductRecordDetentionRecordService sysProductRecordDetentionRecordService;

    @Autowired
    KingDeleteUtil kingDeleteUtil;

    public String createToken() throws IOException {
        String url = baseUrl + "/Token/Create?authorityCode=" + authorityCode;
        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
        Map<Object, Object> map = new HashMap<>();
        HttpEntity<Map<Object, Object>> httpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
        TokenModel tokenModel = jsonObject.getObject("Data", TokenModel.class);
        return tokenModel.getToken();
    }

    public List<TransferModel> getTransferDataList(List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList) throws IOException {
        List<TransferModel> transferModelList = new ArrayList<>();
        for (SysProductRecordDetentionRecord sysProductRecordDetentionRecord : sysProductRecordDetentionRecordList) {

            TransferModel transferModel = new TransferModel();
            Data data = new Data();
            TransferData transferData = new TransferData();
            UpdateTransferData updateTransferData = new UpdateTransferData();

            List<Page1> page1List = new ArrayList<>();

            List<Page2> page2List = new ArrayList<>();

            int i = 1;
            if (sysProductRecordDetentionRecord.getNum() > 0) {
                /***************************Page1开始*******************************/
                Page1 page1 = new Page1();
                /*调拨类型*/
                /*成本调拨 01*/
                FRefType fRefType = new FRefType();
                fRefType.setFName("成本调拨");
                fRefType.setFNumber("01");
                page1.setFRefType(fRefType);

                /*日期*/
                page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", sysProductRecordDetentionRecord.getProductDateInto()));




                /*调拨类型*/
                /*生产入库：311，报废入库：325*/
                FHeadSelfD0136 fHeadSelfD0136 = new FHeadSelfD0136();
                fHeadSelfD0136.setFNumber("311");
                fHeadSelfD0136.setFName("生产入库");
                page1.setFHeadSelfD0136(fHeadSelfD0136);

                /*调出仓库*/
                FSCStockID fscStockID = new FSCStockID();
                fscStockID.setFNumber("IN1.050");
                fscStockID.setFName("成型待入仓");
                page1.setFSCStockID(fscStockID);


                /****************************Page1结束*******************************/
                /****************************Page2开始*******************************/
                Page2 page2 = new Page2();
                /*行号*/
                page2.setFEntryID2(String.valueOf(i));

                /*物料代码*/
                FItemID fItemID = new FItemID();
                fItemID.setFNumber(sysProductRecordDetentionRecord.getProductId());
                fItemID.setFName(sysProductRecordDetentionRecord.getProductId());
                page2.setFItemID(fItemID);
                /*物料名称*/
                page2.setFItemName(sysProductRecordDetentionRecord.getProductName());
                /*基本单位数量*/
                page2.setFQty(String.valueOf(sysProductRecordDetentionRecord.getNum()));
                /*数量*/
                page2.setFauxqty(String.valueOf(sysProductRecordDetentionRecord.getNum()));
                /*调出仓库*/
                FSCStockID1 fscStockID1 = new FSCStockID1();
                fscStockID1.setFNumber("IN1.050");
                fscStockID1.setFName("成型待入仓");
                page2.setFSCStockID1(fscStockID1);

                log.error("机台：{}",JSONObject.toJSONString(sysProductRecordDetentionRecord));
                /*调出仓位*/
                String fscs=null;
                try {
                    if(StringUtils.isNotBlank(sysProductRecordDetentionRecord.getGroupName())&&StringUtils.isNotBlank(sysProductRecordDetentionRecord.getProductRecordShift().getShiftCode())) {
                        fscs = sysProductRecordDetentionRecord.getGroupName() + sysProductRecordDetentionRecord.getProductRecordShift().getShiftCode();
                    }else{
                        fscs="977";
                    }
                } catch (Exception e) {

                    fscs="977";
                }
                FSCSPID fscspid=new FSCSPID();
                fscspid.setFName(fscs);
                fscspid.setFNumber(fscs);
                page2.setFSCSPID(fscspid);

                if (sysProductRecordDetentionRecord.getType().equals("良品")) {

                    /*调入仓库*/
                    FDCStockID fdcStockID = new FDCStockID();
                    FFManagerID ffManagerID = new FFManagerID();
                    FSManagerID fsManagerID = new FSManagerID();
                    FDCStockID1 fdcStockID1 = new FDCStockID1();
                    String productId = sysProductRecordDetentionRecord.getProductId();


                    if (productId.startsWith("1.WC")) {
                        fdcStockID.setFNumber("X03.0");
                        fdcStockID.setFName("X03.0");

                        fdcStockID1.setFNumber("X03.0");
                        fdcStockID1.setFName("X03.0");


                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }
                    else if (productId.startsWith("2.WC")) {

                        fdcStockID.setFNumber("X03.4");
                        fdcStockID.setFName("X03.4");

                        fdcStockID1.setFNumber("X03.4");
                        fdcStockID1.setFName("X03.4");


                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                     }
                    else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                        fdcStockID.setFNumber("X01.0");
                        fdcStockID.setFName("X01.0");

                        fdcStockID1.setFNumber("X01.0");
                        fdcStockID1.setFName("X01.0");


                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID.setFNumber("X01.4");
                        fdcStockID.setFName("X01.4");

                        fdcStockID1.setFNumber("X01.4");
                        fdcStockID1.setFName("X01.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("2.")) {
                        fdcStockID.setFNumber("X06.4");
                        fdcStockID.setFName("X06.4");

                        fdcStockID1.setFNumber("X06.4");
                        fdcStockID1.setFName("X06.4");


                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.")) {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");


                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }
                    page1.setFFManagerID(ffManagerID);
                    page1.setFSManagerID(fsManagerID);
                    page1.setFDCStockID(fdcStockID);
                    page2.setFDCStockID1(fdcStockID1);
                } else if (sysProductRecordDetentionRecord.getType().equals("不良品")) {
                    /*调入仓库*/
                    FDCStockID1 fdcStockID1 = new FDCStockID1();
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");

                    FFManagerID ffManagerID = new FFManagerID();
                    FSManagerID fsManagerID = new FSManagerID();

                    ffManagerID.setFNumber("HJ00177");
                    ffManagerID.setFName("HJ00177");

                    fsManagerID.setFNumber("HJ00177");
                    fsManagerID.setFName("HJ00177");

                    page1.setFFManagerID(ffManagerID);
                    page1.setFSManagerID(fsManagerID);

                    page2.setFDCStockID1(fdcStockID1);
                }


                /*单位*/
                FUnitID fUnitID = new FUnitID();
                fUnitID.setFName("PCS");
                fUnitID.setFNumber("03");
                page2.setFUnitID(fUnitID);

                FDCSPID fdcspid = new FDCSPID();
                fdcspid.setFName("977");
                fdcspid.setFNumber("977");
                page2.setFDCSPID(fdcspid);
                page2.setFSourceBillNo(sysProductRecordDetentionRecord.getWorkPlanId());
                page2.setFnote(fromDateToString("yyyy年MM月dd日", sysProductRecordDetentionRecord.getProductDate()) + sysProductRecordDetentionRecord.getMachineName() + sysProductRecordDetentionRecord.getShift() + "待判入库转" + sysProductRecordDetentionRecord.getType() + sysProductRecordDetentionRecord.getNum() + "PCS");
                if (sysProductRecordDetentionRecord.getKingNo() != null) {
                    if (sysProductRecordDetentionRecord.getType().equals("良品")) {

                        /*调入仓库*/
                        FDCStockID fdcStockID = new FDCStockID();
                        FFManagerID ffManagerID = new FFManagerID();
                        FSManagerID fsManagerID = new FSManagerID();
                        FDCStockID1 fdcStockID1 = new FDCStockID1();
                        String productId = sysProductRecordDetentionRecord.getProductId();


                        if (productId.startsWith("1.WC")) {
                            fdcStockID.setFNumber("X03.0");
                            fdcStockID.setFName("X03.0");

                            fdcStockID1.setFNumber("X03.0");
                            fdcStockID1.setFName("X03.0");


                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.WC")) {

                            fdcStockID.setFNumber("X03.4");
                            fdcStockID.setFName("X03.4");

                            fdcStockID1.setFNumber("X03.4");
                            fdcStockID1.setFName("X03.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                         } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                            fdcStockID.setFNumber("X01.0");
                            fdcStockID.setFName("X01.0");

                            fdcStockID1.setFNumber("X01.0");
                            fdcStockID1.setFName("X01.0");


                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.YD117")) {

                            fdcStockID.setFNumber("X01.4");
                            fdcStockID.setFName("X01.4");

                            fdcStockID1.setFNumber("X01.4");
                            fdcStockID1.setFName("X01.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("2.")) {
                            fdcStockID.setFNumber("X06.4");
                            fdcStockID.setFName("X06.4");

                            fdcStockID1.setFNumber("X06.4");
                            fdcStockID1.setFName("X06.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.")) {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");


                            fdcStockID1.setFNumber("X06.0");
                            fdcStockID1.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            fdcStockID1.setFNumber("X06.0");
                            fdcStockID1.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        }
                        page1.setFFManagerID(ffManagerID);
                        page1.setFSManagerID(fsManagerID);
                        page1.setFDCStockID(fdcStockID);
                        page2.setFDCStockID1(fdcStockID1);
                        page1.setFBillNo(sysProductRecordDetentionRecord.getKingNo());
                    } else if (sysProductRecordDetentionRecord.getType().equals("不良品")) {
                        /*调入仓库*/
                        FDCStockID fdcStockID = new FDCStockID();
                        fdcStockID.setFNumber("WF5.BF5");
                        fdcStockID.setFName("报废仓");
                        page1.setFDCStockID(fdcStockID);


                        FFManagerID ffManagerID = new FFManagerID();
                        FSManagerID fsManagerID = new FSManagerID();

                        ffManagerID.setFNumber("HJ00177");
                        ffManagerID.setFName("HJ00177");

                        fsManagerID.setFNumber("HJ00177");
                        fsManagerID.setFName("HJ00177");

                        page1.setFFManagerID(ffManagerID);
                        page1.setFSManagerID(fsManagerID);

                        /*调入仓库*/
                        FDCStockID1 fdcStockID1 = new FDCStockID1();
                        fdcStockID1.setFNumber("WF5.BF5");
                        fdcStockID1.setFName("报废仓");
                        page2.setFDCStockID1(fdcStockID1);
                        page1.setFBillNo(sysProductRecordDetentionRecord.getKingNo());
                    }
                    updateTransferData.setFBillNo(sysProductRecordDetentionRecord.getKingNo());

                    page1List.add(page1);
                    page2List.add(page2);
                    data.setPage1(page1List);
                    data.setPage2(page2List);
                    updateTransferData.setData(data);
                    transferModel.setUpdateTransferData(updateTransferData);
                    transferModel.setProductRecordDetentionRecord(sysProductRecordDetentionRecord);
                    transferModel.setWay(false);

                } else {
                    if (sysProductRecordDetentionRecord.getType().equals("良品")) {
                        /*调入仓库*/
                        FDCStockID fdcStockID = new FDCStockID();
                        FFManagerID ffManagerID = new FFManagerID();
                        FSManagerID fsManagerID = new FSManagerID();
                        FDCStockID1 fdcStockID1 = new FDCStockID1();
                        String productId = sysProductRecordDetentionRecord.getProductId();


                        if (productId.startsWith("1.WC")) {
                            fdcStockID.setFNumber("X03.0");
                            fdcStockID.setFName("X03.0");

                            fdcStockID1.setFNumber("X03.0");
                            fdcStockID1.setFName("X03.0");


                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.WC")) {

                            fdcStockID.setFNumber("X03.4");
                            fdcStockID.setFName("X03.4");

                            fdcStockID1.setFNumber("X03.4");
                            fdcStockID1.setFName("X03.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                         } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                            fdcStockID.setFNumber("X01.0");
                            fdcStockID.setFName("X01.0");

                            fdcStockID1.setFNumber("X01.0");
                            fdcStockID1.setFName("X01.0");


                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.YD117")) {

                            fdcStockID.setFNumber("X01.4");
                            fdcStockID.setFName("X01.4");

                            fdcStockID1.setFNumber("X01.4");
                            fdcStockID1.setFName("X01.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("2.")) {
                            fdcStockID.setFNumber("X06.4");
                            fdcStockID.setFName("X06.4");

                            fdcStockID1.setFNumber("X06.4");
                            fdcStockID1.setFName("X06.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.")) {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");


                            fdcStockID1.setFNumber("X06.0");
                            fdcStockID1.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            fdcStockID1.setFNumber("X06.0");
                            fdcStockID1.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        }
                        page1.setFFManagerID(ffManagerID);
                        page1.setFSManagerID(fsManagerID);
                        page1.setFDCStockID(fdcStockID);
                        page2.setFDCStockID1(fdcStockID1);


                    }
                    else if (sysProductRecordDetentionRecord.getType().equals("不良品")) {
                        /*调入仓库*/
                        FDCStockID fdcStockID = new FDCStockID();
                        fdcStockID.setFNumber("WF5.BF5");
                        fdcStockID.setFName("报废仓");
                        page1.setFDCStockID(fdcStockID);

                        /*调入仓库*/
                        FDCStockID1 fdcStockID1 = new FDCStockID1();
                        fdcStockID1.setFNumber("WF5.BF5");
                        fdcStockID1.setFName("报废仓");


                        FFManagerID ffManagerID = new FFManagerID();
                        FSManagerID fsManagerID = new FSManagerID();

                        ffManagerID.setFNumber("HJ00177");
                        ffManagerID.setFName("HJ00177");

                        fsManagerID.setFNumber("HJ00177");
                        fsManagerID.setFName("HJ00177");

                        page1.setFFManagerID(ffManagerID);
                        page1.setFSManagerID(fsManagerID);

                        page2.setFDCStockID1(fdcStockID1);
                        page2.setFDCStockID1(fdcStockID1);


                    }
                    page1List.add(page1);
                    page2List.add(page2);
                    data.setPage1(page1List);
                    data.setPage2(page2List);
                    transferData.setData(data);
                    transferModel.setTransferData(transferData);
                    transferModel.setProductRecordDetentionRecord(sysProductRecordDetentionRecord);
                    transferModel.setWay(true);

                }
                transferModelList.add(transferModel);
            } else {
                if (sysProductRecordDetentionRecord.getKingNo() != null) {
                    log.error("为零删除：{}", sysProductRecordDetentionRecord.getKingNo());
                    this.kingDeleteUtil.deleteTransferByNo(sysProductRecordDetentionRecord.getKingNo());
                }
            }
            /****************************Page2结束*******************************/


        }
        return transferModelList;
    }

    public Result<?> addAndUpdateTransferData1(TransferModel transferModel) throws IOException {
        if (transferModel.isWay()) {
            KingdeeResult kingdeeResult = add(transferModel);
            log.error("TransferData:{}",JSONObject.toJSONString(transferModel));
            log.error("添加结果：{}", JSONObject.toJSONString(kingdeeResult));
        } else {


            KingdeeResult kingdeeResult = this.update(transferModel);
            log.error("更新结果：{}", JSONObject.toJSONString(kingdeeResult));
            if (kingdeeResult.getStatusCode() == 1402) {
                log.error("无单据添加");
                QueryWrapper queryWrapper = QueryWrapper.create();
                queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.KING_NO.eq(kingdeeResult.getData()));
                List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList = this.sysProductRecordDetentionRecordService.list(queryWrapper);
                for (SysProductRecordDetentionRecord productRecordDetentionRecord : sysProductRecordDetentionRecordList) {
                    productRecordDetentionRecord.setKingNo(null);
                    productRecordDetentionRecord.setKingId(null);
                }
                List<TransferModel> transferModelList = getTransferDataList(sysProductRecordDetentionRecordList);
                for (TransferModel transferModel1 : transferModelList) {
                    addAndUpdateTransferData1(transferModel1);
                }

            }

        }
        return ResultTool.success();
    }

    public KingdeeResult add(TransferModel transferModel) throws IOException {
        TransferData transferData = transferModel.getTransferData();
        if (transferData != null) {
            String token = this.createToken();

            String url = baseUrl + "/Transfer/Save?token=" + token;
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<TransferData> httpEntity = new HttpEntity<>(transferData, headers);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
            log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
            KingdeeResult kingdeeResult = JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
            this.updateAllProductRecord(kingdeeResult, transferModel.getProductRecordDetentionRecord());
            return kingdeeResult;
        } else {
            return null;
        }
    }

    public KingdeeResult update(TransferModel transferModel) throws IOException {
        UpdateTransferData updateTransferData = transferModel.getUpdateTransferData();
        log.error("Update:{}", updateTransferData);

        if (updateTransferData != null) {
            String token = this.createToken();
            String url = baseUrl + "/Transfer/Update?token=" + token;
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<UpdateTransferData> httpEntity = new HttpEntity<>(updateTransferData, headers);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(responseEntity));
            log.error("Json:{}", JSONObject.toJSONString(jsonObject.getJSONObject("body")));
            KingdeeResult kingdeeResult = JSONObject.parseObject(JSONObject.toJSONString(jsonObject.getJSONObject("body")), KingdeeResult.class);
            this.updateAllProductRecord(kingdeeResult, transferModel.getProductRecordDetentionRecord());
            return kingdeeResult;
        } else {
            return null;
        }
    }


    public Result<?> updateAllProductRecord(KingdeeResult kingdeeResult, SysProductRecordDetentionRecord productRecordDetentionRecord) {
        if (kingdeeResult.getStatusCode() == 200) {
            JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
            log.error("JSONOBject:{}", jsonObject);
            try {
                String kingNo = jsonObject.getString("BillNo");
                String kingId = jsonObject.getString("BillInterID");

                productRecordDetentionRecord.setKingId(kingId);
                productRecordDetentionRecord.setKingNo(kingNo);

            } catch (Exception e) {

            }
            this.sysProductRecordDetentionRecordService.updateById(productRecordDetentionRecord);
            return ResultTool.success();
        } else {
            return ResultTool.fail();
        }

    }


}
